package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AComment;
import com.ruoyi.system.service.IACommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 评论管理Controller
 *
 * @author system
 * @date 2025-02-23
 */
@RestController
@RequestMapping("/system/aComment")
public class ACommentController extends BaseController {
    @Autowired
    private IACommentService aCommentService;

    /**
     * 查询评论管理列表
     */
    @GetMapping("/list")
    public AjaxResult list(AComment aComment) {
        List<AComment> list = aCommentService.selectACommentList(aComment);
        return success(list);
    }

    /**
     * 导出评论管理列表
     */
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AComment aComment) {
        List<AComment> list = aCommentService.selectACommentList(aComment);
        ExcelUtil<AComment> util = new ExcelUtil<>(AComment.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aCommentService.selectACommentById(id));
    }

    /**
     * 新增评论管理
     */
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AComment aComment) {
        if (!aCommentService.checkNameUnique(aComment)) {
            return error("新增分类'" + aComment.getComName() + "'失败，分类名称已存在");
        }
        aComment.setCreateBy(getUsername());
        return toAjax(aCommentService.insertAComment(aComment));
    }

    /**
     * 修改分类
     */
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AComment aComment) {
        Long id = aComment.getId();
        if (!aCommentService.checkNameUnique(aComment)) {
            return error("修改分类'" + aComment.getComName() + "'失败，分类名称已存在");
        }
        else if (aComment.getParentId().equals(id)) {
            return error("修改分类'" + aComment.getComName() + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, aComment.getDelFlag()) && aCommentService.selectNormalChildrenCategoryById(id) > 0) {
            return error("该分类包含未停用的子分类！");
        }
        aComment.setUpdateBy(getUsername());
        return toAjax(aCommentService.updateAComment(aComment));
    }

    /**
     * 删除评论管理
     */
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/logicDel/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        AComment aComment = new AComment();
        aComment.setParentId(id);
        aComment.setDelFlag("0");
        List<AComment> categories = aCommentService.selectACommentList(aComment);
        if(CollectionUtils.isNotEmpty(categories)){
            return error("存在下级分类，不允许删除");
        }
        //todo 是否已使用
        /*GfXxx xxx = new GfXxx();
        xxx.setCategoryId(id);
        List<GfXxx> xxxList = gfXxxService.selectGfXxxList(xxx);
        if(CollectionUtils.isNotEmpty(xxxList)){
            return error("分类已使用，不允许删除");
        }*/
//        AComment category = aCommentService.selectACommentById(id);
//        category.setDelFlag("2");
        return toAjax(aCommentService.deleteACommentById(id));
    }

}
